package com.woniu.micropropertyserver.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.woniu.microcommonsserver.entity.OwnerInfoEntity;
import com.woniu.micropropertyserver.bean.DeployTeIc;
import com.woniu.micropropertyserver.bean.DeployTenement;
import com.woniu.micropropertyserver.mapper.OwnerMapper;
import com.woniu.micropropertyserver.mapper.TenementIcMapper;
import com.woniu.micropropertyserver.service.ITenementIcCheckService;
import com.woniu.micropropertyserver.util.PageRest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Transactional(readOnly = true)
@Service
public class TenementIcCheckServiceImpl implements ITenementIcCheckService {

    @Resource
    private TenementIcMapper tenementIcMapper;
    @Resource
    private OwnerMapper ownerMapper;
    @Override
    public PageInfo<DeployTeIc> findByParams(PageRest pageRest, DeployTeIc check) {


        return PageHelper.startPage(pageRest.getPageNum(),pageRest.getPageSize()).doSelectPageInfo(()->tenementIcMapper.findByParams(pageRest,check));
    }



    @Transactional(isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    @Override
    public int delInfo(Long[] ids) {
        return tenementIcMapper.delInfo(ids);
    }


    @Transactional(isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    @Override
    public int updateAuth(Long[] ids, Date authorizeStartDate, Date authorizeEndDate) {
        Date now = new Date();
        return tenementIcMapper.updateAuth(ids,authorizeStartDate,authorizeEndDate,now);
    }

    @Transactional(isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    @Override
    public int saveInfo(DeployTeIc deployTeIc) {

            long id=0l;

        if(deployTeIc.getName()!=null && deployTeIc.getHouseNum()!=null){
            OwnerInfoEntity owner =  ownerMapper.findByNameAndHouse(deployTeIc.getName(),deployTeIc.getHouseNum());

            if(owner!=null ){
                id=owner.getId();
            }
            deployTeIc.setFkOwnerId(id);

        }
        return tenementIcMapper.saveInfo(deployTeIc);
    }

    @Transactional(isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    @Override
    public int updateInfo(DeployTeIc deployTeIc) {
        if(deployTeIc.getName()!=null && deployTeIc.getHouseNum()!=null){
           Long id=0l;
          OwnerInfoEntity owner =  ownerMapper.findByNameAndHouse(deployTeIc.getName(),deployTeIc.getHouseNum());

               if(owner!=null ){
                   id=owner.getId();
               }
               deployTeIc.setFkOwnerId(id);

           }
        Date now = new Date();
        return tenementIcMapper.updateInfo(deployTeIc,now);
    }

    @Override
    public DeployTeIc findById(long id) {
        return tenementIcMapper.findById(id);
    }

}
